package com.peanut.baby.mvp.livedetail.detail;

import android.app.Activity;

import com.peanut.baby.InitManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseResponse;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.LiveRoom;
import com.peanut.baby.model.ShareType;
import com.peanut.baby.model.VarPoint;
import com.peanut.baby.mvp.livedetail.LiveDetailContract;
import com.tencent.mars.xlog.Log;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;

/**
 * Created by anonymouslystudio on 2017/7/21.
 */

public class LiveDetailFragmentPresenter implements LiveDetailFragmentContract.Presenter {
    private static final String TAG = LiveDetailFragmentPresenter.class.getSimpleName();
    private LiveDetailFragmentContract.View view;
    private Activity activity;

    public LiveDetailFragmentPresenter(LiveDetailFragmentContract.View view, Activity activity) {
        this.view = view;
        this.activity = activity;
    }

    @Override
    public void joinRoom(String roomId) {
        Observable observable = RetrofitClient.getInstance().enroleLiveRoom(InitManager.getInstance().getUser().userToken,
                InitManager.getInstance().getUserId(), roomId);
        observable.compose(BaseScheduler.<BaseResponse<String>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    protected void onHandleSuccess(String o) {
                        view.onJoinSuccess();

                    }

                    @Override
                    protected void onHandleError(String code, String msg) {
                        view.onRequestFailed(code, msg);
                    }

                });
    }

    @Override
    public void sharePoint(String roomId) {
        Observable obs = RetrofitClient.getInstance().sharePoint(InitManager.getInstance().getUserId(),
                ShareType.TYPE_SHARE_LIVE + "", roomId);
        obs.compose(BaseScheduler.<VarPoint>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<VarPoint>() {
                    @Override
                    protected void onHandleSuccess(VarPoint o) {
                        InitManager.getInstance().getUser().userPoint = o.totalPoint;
                        InitManager.getInstance().updateLocalUser();
                        view.onSharePointSuccess("花生壳加" + o.variedPoint + ",总花生壳" + o.totalPoint);
                    }

                    @Override
                    protected void onHandleError(String code, String msg) {
                        Log.w(TAG, "sharePoint failed " + msg);
//                view.onSharePointSuccess(msg);
                    }
                });
    }
}
